<?php
namespace Admin\Controller;
use Think\Controller;
class UnitController extends BaseController {
    public function index(){
        if(I("get.name")!==null && I("get.name")!==''){
            $where['unit_name']=I("get.name");
        }else{
            $where="1=1";
        }
        $unit = M('unit');
        $count      = $unit->where($where)->count();
        $Page       = new \Think\Page($count,15);
        $show       = $Page->show();
        $list = $unit->where($where)->limit($Page->firstRow.','.$Page->listRows)->order("id desc")->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->assign('count',$count);
        $this->display();
    }
    public function addD($name,$code){
        if($name==''){
            alert(0,'单位名称不能为空');
        }
        if($code==''){
            alert(0,'单位编码不能为空');
        }
        $data['unit_name']=$name;
        $data['unit_no']=$code;
        $data['_logic'] = 'or';
        if(M("unit")->where($data)->count()>0){
            alert(0,'单位名称或者单位编码已经存在');
        }
        unset($data['_logic']);
        $data['operator']=session('admin');
        $result= M("unit")->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function editD($id,$name){
        if($name==''){
            alert(0,'单位名称不能为空');
        }
        if($id==''){
            alert(0,'操作有误');
        }
        $data['name']=trim($name);
        $massage=session('company');
        $data['operator']=$massage['username'];
        $result= M("unit")->where("id='".$id."'")->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function del($id){
        if($id==''){
            alert(0,'操作有误');
        }
        $group_id=explode('@',$id);
        $id='';
        foreach($group_id as $v){
            if($v!==''){
                $id.=','.$v;
            }
        }
        $result= M("unit")->delete(ltrim($id,','));
        if($result){
            alert(1,'删除成功');
        }else{
            alert(0,'删除失败');
        }
    }
}